ige.controller('EmployeeController', ['$scope', 'EmployeeService', '$mdToast', '$state', 'EventBus', '$timeout', '$mdDialog',
    function ($scope, EmployeeService, $mdToast, $state, EventBus, $timeout, $mdDialog) {
        var pageSkip = 0;
        var currentDeleteId = 0;
        var deleteArray = [];

        var timer;
        $scope.noNewData = false;
        EventBus.off("Employee.keywordSearch");
        EventBus.on("Employee.keywordSearch", function (event) {
            $timeout.cancel(timer);
            timer = $timeout(
                function () {
                    pageSkip = 0;
                    $scope.Employees = [];
                    $scope.filter = event.data;
                    $scope.getEmployees();
                }, 300
            );
        });

        $scope.$on(
            "$destroy",
            function (event) {
                $timeout.cancel(timer);
            }
        );

        $scope.Employees = [];
        $scope.loadMore = false;
        $scope.loaddingListData = true;
        $scope.getEmployees = function () {
            $scope.loaddingListData = true;
            EmployeeService.getEmployees(pageSkip, ige.systemSetting.defaultPageSize, $scope.filter, function (data) {
                $scope.Employees = $scope.Employees.concat(data);
                if (data.length == 0) {
                    $scope.noNewData = true;
                } else {
                    $scope.noNewData = false;
                }
            }).then(function () {
                pageSkip += ige.systemSetting.defaultPageSize;
            }).then(function () {
                $scope.loaddingListData = false;
            });
        };

        $scope.loadMoreData = function () {
            $scope.loadMore = true;
            EmployeeService.getEmployees(pageSkip, ige.systemSetting.defaultPageSize, $scope.filter, function (data) {
                $scope.Employees = $scope.Employees.concat(data);
                if (data.length == 0) {
                    $scope.noNewData = true;
                } else {
                    $scope.noNewData = false;
                }
            }).then(function () {
                pageSkip += ige.systemSetting.defaultPageSize;
            }).then(function () {
                $scope.loadMore = false;
            });
        };
        $scope.detail = function (id) {
            $state.go('MainNav.DetailEmployee', {id: id});
        };
        $scope.edit = function (id) {
            $state.go('MainNav.EditEmployee', {id: id});
        };
        $scope.getEmployees();
        $scope.itemHide = function (id) {
            return deleteArray.indexOf(id) > -1;
        };

        $scope.loadding = function (id) {
            return id == currentDeleteId;
        };
        var deleteSuccess = false;
        $scope.delete = function (id) {
            currentDeleteId = id;
            EmployeeService.deleteEmployee(id, function (data) {
                deleteSuccess = true;
            }).then(function () {
                if (deleteSuccess) {
                    currentDeleteId = 0;
                    deleteArray.push(id);
                }
                deleteSuccess = false;
            });
        };

        $scope.selected = [];
        $scope.toggle = function (item, list) {
            var idx = list.indexOf(item);
            if (idx > -1) list.splice(idx, 1);
            else list.push(item);
        };
        $scope.exists = function (item, list) {
            return list.indexOf(item) > -1;
        };

        $scope.toggleAll = function () {
            if ($scope.selected.length != 0) {
                $scope.selected = [];
            } else {
                for (var inx = 0; inx < $scope.Employees.length; inx++) {
                    $scope.selected.push($scope.Employees[inx].id);
                }
            }
        };
        $scope.existsAll = function () {
            return $scope.selected.length == $scope.Employees.length;
        };


        EventBus.off("Employee.RewardEmployees");
        EventBus.on("Employee.RewardEmployees", function (event) {
            if (!$scope.selected || $scope.selected.length == 0) {
                $mdDialog.show(
                    $mdDialog.alert()
                        .clickOutsideToClose(true)
                        .title('提示')
                        .textContent('请选择需要计算的员工')
                        .ok('确定')
                )
            } else {
                $mdDialog.show({
                    controller: 'RewardShowController',
                    templateUrl: 'Employee/_select_reward_method.html',
                    parent: angular.element('body'),
                    targetEvent: event.data,
                    clickOutsideToClose: true,
                    fullscreen: true
                }).then(function (param) {
                    $state.go('MainNav.EmployeeReward', {
                        methodid: param.rewardMethodId,
                        resultid: param.resultMethodId,
                        rewardAvgId: param.RewardAvgId,
                        ids: $scope.selected
                    });
                });
            }
        });
    }]);


ige.controller('AddEmployeeController', ['$scope', 'EmployeeService', '$mdToast', '$state', 'DepartmentService',
    function ($scope, EmployeeService, $mdToast, $state, DepartmentService) {
        $scope.loadding = false;
        $scope.Departments = null;
        var addSuccess = false;
        $scope.Employee = {
            Name: undefined,
            Description: undefined,
            Phone: undefined,
            Email: undefined,
            Password: undefined,
            DepartmentId: undefined,
            Salary: undefined,
            Reward: undefined,
            Insurances: 0,
            Adjust: 0
        };

        $scope.addEmployee = function () {
            $scope.loadding = true;
            EmployeeService.addEmployee($scope.Employee, function (data) {
                addSuccess = true;
            }).then(function () {
                if (addSuccess) {
                    $state.go('MainNav.Employee');
                } else {
                }
                $scope.loadding = false;
                addSuccess = false;
            });
        };


        DepartmentService.getDepartments(null, null, null, function (data) {
            $scope.Departments = data;
        });

    }]);


ige.controller('EditEmployeeController', ['$scope', 'EmployeeService', 'DepartmentService', '$mdToast', '$state', '$stateParams',
    function ($scope, EmployeeService, DepartmentService, $mdToast, $state, $stateParams) {
        $scope.loadding = false;
        var id = $stateParams.id;
        var editSuccess = false;
        $scope.Employee = {
            Id: id,
            Name: undefined,
            Description: undefined,
            Phone: undefined,
            Email: undefined,
            Password: undefined,
            DepartmentId: undefined,
            Salary: undefined,
            Reward: undefined,
            Insurances: 0,
            Adjust: 0
        };


        DepartmentService.getDepartments(null, null, null, function (data) {
            $scope.Departments = data;
        });


        EmployeeService.getEmployee(id, function (data) {
            $scope.Employee.Name = data.name;
            $scope.Employee.Description = data.description;
            $scope.Employee.Phone = data.phone;
            $scope.Employee.Email = data.email;
            $scope.Employee.DepartmentId = data.departmentid;
            $scope.Employee.Password = data.password;
            $scope.Employee.Salary = parseFloat(data.salary);
            $scope.Employee.Reward = parseFloat(data.reward);
            $scope.Employee.Insurances = parseFloat(data.insurances);
            $scope.Employee.Adjust = parseFloat(data.adjust);
        });


        $scope.editEmployee = function () {
            $scope.loadding = true;
            EmployeeService.editEmployee($scope.Employee, function (data) {
                editSuccess = true;
            }).then(function () {
                if (editSuccess) {
                    $state.go('MainNav.Employee');
                }
                $scope.loadding = false;
                editSuccess = false
            });
        }

    }]);


ige.controller('DetailEmployeeController', ['$scope', 'EmployeeService', 'DepartmentService', '$mdToast', '$state', '$stateParams', 'EventBus', '$interval', '$mdDialog',
    function ($scope, EmployeeService, DepartmentService, $mdToast, $state, $stateParams, EventBus, $interval, $mdDialog) {
        $scope.loadding = false;
        var id = $stateParams.id;

        $scope.result = [];
        //var startComputerSuccess = false;
        //var computerId = null;
        //var isDone = false;
        //var timer = null;
        //var progressInter = null;
        //var size = ige.systemSetting.batchDataSize;
        //$scope.resultItem = [];
        //$scope.rewardMethodId = null;
        //$scope.rewardMethodName = null;
        //$scope.total = 0;
        //$scope.Employee = {};
        //$scope.rewardMethodId = null;
        //$scope.computting = false;
        //
        //var fetchBatchIndicate = 0;
        //var processProgress = function (data) {
        //    for(var inx=0;inx<data.length;inx++){
        //        var item = data[inx];
        //        $scope.resultItem.push({
        //            Index: item.index,
        //            Salary: item.salary,
        //            Reward: item.reward,
        //            Tax: item.tax
        //        });
        //        $scope.total++;
        //    }
        //};


        EventBus.off("Employee.RewardEmployee");
        EventBus.on("Employee.RewardEmployee", function (event) {
            $mdDialog.show({
                controller: 'RewardShowController',
                templateUrl: 'Employee/_select_reward_method.html',
                parent: angular.element('body'),
                targetEvent: event.data,
                clickOutsideToClose: true,
                fullscreen: true
            }).then(function (param) {
                //processProgress();
                $scope.loadding = true;
                $scope.result = [];
                EmployeeService.getbestschemeApi(id, param.rewardMethodId, param.resultMethodId, param.RewardAvgId, function (data) {
                    for (var inx = 0; inx < data.ideal.length; inx++) {
                        $scope.result.push({
                            Reward: data.ideal[inx].reward,
                            Salary: data.ideal[inx].salary,
                            Tax: data.ideal[inx].tax,
                            Actual: data.actual.tax,
                            Sub: data.actual.tax - data.ideal[inx].tax
                        });
                    }
                    $scope.loadding = false;
                });


                //EmployeeService.computerReward(id, answer, function (data) {
                //    startComputerSuccess = true;
                //    if (data && data.computerId) {
                //        computerId = data.computerId;
                //    }
                //}).then(function () {
                //    if (startComputerSuccess) {
                //        timer = $interval(function () {
                //            if (!isDone) {
                //                EmployeeService.getRewardProgress(fetchBatchIndicate * size, size, function (data) {
                //                    processProgress(data);
                //                }, function () {
                //                    isDone = true;
                //                });
                //
                //            } else {
                //                fetchBatchIndicate = 1;
                //                $interval.cancel(timer);
                //                startComputerSuccess = false;
                //                $scope.computting = false;
                //                isDone = false;
                //            }
                //            fetchBatchIndicate++;
                //        }, 100);
                //    } else {
                //        startComputerSuccess = false;
                //        $scope.computting = false;
                //        isDone = false;
                //    }
                //}, function () {
                //    console.log('123');
                //});
            });
        });

        //$scope.$on(
        //    "$destroy",
        //    function (event) {
        //        $interval.cancel(timer);
        //    }
        //);

        EmployeeService.getEmployee(id, function (data) {
            $scope.Employee.Name = data.name;
            $scope.Employee.Description = data.description;
            $scope.Employee.Phone = data.phone;
            $scope.Employee.Email = data.email;
            $scope.Employee.DepartmentId = data.departmentid;
            $scope.Employee.Password = data.password;
            $scope.Employee.Salary = parseFloat(data.salary);
            $scope.Employee.Reward = parseFloat(data.reward);
            $scope.Employee.Insurances = parseFloat(data.insurances);
            $scope.Employee.Adjust = parseFloat(data.adjust);
        });
    }]);
ige.controller('EmployeeRewardController', ['$scope', 'EmployeeService', '$state', '$stateParams', 'EventBus', '$mdDialog',
    function ($scope, EmployeeService, $state, $stateParams, EventBus, $mdDialog) {
        $scope.loadding = true;
        $scope.result = [];
        var ids = $stateParams.ids;
        var rewardMethodId = $stateParams.methodid;
        var resultMethodId = $stateParams.resultid;
        var rewardAvgId = $stateParams.rewardAvgId;
        var path = undefined;
        $scope.total = {
            reward: 0,
            salary: 0,
            oritax: 0,
            newtax: 0,
            subtotal: 0
        };

        EmployeeService.getbestschemesApi(ids.split(','), rewardMethodId, resultMethodId, rewardAvgId, function (data) {
            path = data.path;
            for (var inx = 0; inx < data.tax.length; inx++) {

                if (data.tax[inx].actual == null) {
                    $scope.result.push({
                        Employeename: data.tax[inx].employeename,
                        Reward: 0,
                        Salary: 0,
                        Tax: 0,
                        Sub: 0,
                        ActualReward: 0,
                        ActualSalary: 0,
                        ActualTax: 0,
                        Note: '该员工信息部完全，请补全信息再次计算'
                    });
                } else {
                    $scope.result.push({
                        Employeename: data.tax[inx].employeename,
                        Reward: data.tax[inx].reward,
                        Salary: data.tax[inx].salary,
                        Tax: data.tax[inx].tax,
                        ActualReward: data.tax[inx].actual.reward,
                        ActualSalary: data.tax[inx].actual.salary,
                        ActualTax: data.tax[inx].actual.tax,
                        Sub: data.tax[inx].actual.tax - data.tax[inx].tax,
                        Note: '最优'
                    });

                    $scope.total.reward += data.tax[inx].reward;
                    $scope.total.salary += data.tax[inx].salary;
                    $scope.total.newtax += data.tax[inx].tax;
                    $scope.total.oritax += data.tax[inx].actual.tax;
                    $scope.total.subtotal = $scope.total.oritax - $scope.total.newtax;
                }
            }

            $scope.loadding = false;
        });

        EventBus.off("Employee.DownloadRewardEmployees");
        EventBus.on("Employee.DownloadRewardEmployees", function (event) {
            document.location.href = ige.systemSetting.apiDomain + path;
        });
    }]);